import { getSysStructDetailApi, getSysStructsTreeApi, createOrEditDepartmentApi } from './create-department.service';
import { alertHtmlWithCallback, alertMessage, showAlertMessage } from '@/utils/utils';
export default {
  name: 'create-department',
  components: {},
  directives: {},
  props: {
    type: {
      type: String,
      default: 'create'
    },
    struct: {
      type: Object,
      default: () => null
    }
  },
  data() {
    return {
      loading: false, // table loading
      showDialog: true, // 是否显示弹窗
      structs: [], // 上级部门列表
      title: '创建部门',
      editForm: {
        isSection: 1
      }, // 编辑用户表单
      editRules: {
        code: [
          { required: true, message: '此项为必填项', trigger: "change" },
          { pattern: /^[a-zA-Z0-9]+$/, message: '此项限字母或数字', trigger: 'blur' },
          { min: 1, max: 6, message: '此项长度限6位', trigger: 'blur' }
        ],
        pidObj: [{ required: true, message: '此项为必填项', trigger: "change" }],
        name: [
          { required: true, message: '此项为必填项', trigger: "change" },
          { pattern: /^[a-zA-Z0-9\u4E00-\u9FFF]+$/, message: '此项限字母,数字或汉字', trigger: 'blur' },
          { min: 1, max: 20, message: '此项长度限20位', trigger: 'blur' }
        ],
        description: [
          { min: 1, max: 20, message: '此项长度限20位', trigger: 'blur' }
        ]
      } // 编辑表单验证规则
    }
  },
  created() {
    // 创建子部门
    if (this.type === 'create') {
      this.title = '创建部门';
      this.$set(this.editForm, 'pidObj', this.struct)
    }
    // 编辑当前部门
    if (this.type === 'edit' || this.type === 'editTree') {
      this.title = '编辑部门';
      this.getSysStructDetail();
    }
    this.getSysStruct();
  },
  methods: {
    // 获取组织树
    getSysStruct() {
      getSysStructsTreeApi().then(res => {
        this.structs = res.data[0].children
      });
    },

    // 获取部门详情
    getSysStructDetail() {
      getSysStructDetailApi(this.struct.objectId).then(res => {
        res.data.pidObj = { objectId: res.data.pid, name: res.data.superName }
        this.editForm = res.data
      })
    },

    // 创建或者编辑信息
    save() {
      // 表单验证
      let message = {
        name: this.editForm.name
      }
      if (this.type !== 'editTree') {
        message.code = this.editForm.code;
        message.pidNames = this.editForm.pidObj.name;
      }
      const alertMsg = showAlertMessage(`请确认${this.type === 'create' ? '创建' : '编辑'}内容`, message)
      alertHtmlWithCallback(alertMsg, `确认要${this.type === 'create' ? '创建' : '编辑'}这条信息吗?`).then(yes => {
        if (yes) {
          this.loading = true;
          this.$refs.dataForm.validate(valid => {
            if (valid) {
              this.editForm.pid = this.editForm.pidObj.objectId
              createOrEditDepartmentApi(this.editForm).then((res) => {
                this.loading = false;
                alertMessage(res.message);
                this.showDialog = false;
                this.$emit('reload', true)
              }).catch(() => { }); // 关闭confirm框的回调，不处理的话会报一个js error
            } else {
              this.loading = false
            }
          });
        }
      })
    },

    // 关闭dialog时需要告诉父组件隐藏弹窗
    closeDialog() {
      this.showDialog = false;
      this.$emit('close', true);
    }
  }
}
